﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseLook : MonoBehaviour
{

    public enum RotationAxex
    {
        MouseXAndY = 0,
        MouseX = 1,
        MouseY = 2
    }

    public RotationAxex axes = RotationAxex.MouseXAndY;
    //旋转速度
    public float sensitiveityHor = 9.0f;
    public float sensitiveityVert = 9.0f;

    //垂直旋转最大和最小值
    public float minimumVert = -45.0f;
    public float maximumVert = 45.0f;

    //垂直角度
    private float _rotationX = 0;

    // Use this for initialization
    void Start()
    {
        Rigidbody body = GetComponent<Rigidbody>();
        if (body != null)
        {
            body.freezeRotation = true;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (axes == RotationAxex.MouseX)
        {
            transform.Rotate(0, Input.GetAxis("Mouse X") * sensitiveityHor, 0);
        }
        else if (axes == RotationAxex.MouseY)
        {
            //基于鼠标增加垂直角度
            _rotationX -= Input.GetAxis("Mouse Y") * sensitiveityVert;
            _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);

            //保持Y的角度一样
            float rotationY = transform.localEulerAngles.y;
            transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
        }
        else
        {
            _rotationX -= Input.GetAxis("Mouse Y") * sensitiveityVert;
            _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);

            //旋转变化量
            float delta = Input.GetAxis("Mouse X") * sensitiveityHor;
            //使用delta递增旋转角度
            float rotationY = transform.localEulerAngles.y + delta;

            transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
        }
    }
}